package main

import (
	"fmt"
	"net/rpc"

	"gitee.com/lcc_dr/rpc/rpc_interface/service"
)

/*
1、自定义一个函数
2、注册到rpc
3、rpc 启动

*/
var _ service.HelloService = (*HelloServiceClient)(nil)

type HelloServiceClient struct {
	Client *rpc.Client
	/* * rpc.Client 本质是一个Client的结构体

		type Client struct {
		codec ClientCodec

		reqMutex sync.Mutex // protects following
		request  Request

		mutex    sync.Mutex // protects following
		seq      uint64
		pending  map[uint64]*Call
		closing  bool // user has called Close
		shutdown bool // server has told us to stop
	}
	*/
}

func RpcDial(network, addr string) (*HelloServiceClient, error) {
	client, err := rpc.Dial(network, addr)
	if err != nil {
		panic(err)
	}
	return &HelloServiceClient{Client: client}, nil

}

func (h *HelloServiceClient) Hello(res string, response *string) error {

	err := h.Client.Call(service.SERVICE_NAME+".Hello", res, &response)
	if err != nil {
		panic(err)
	}
	//fmt.Println(&response)

	return nil

}

func main() {

	client, err := RpcDial("tcp", "localhost:1234")
	if err != nil {
		panic(err)
	}

	var reply string
	err = client.Hello("lcc", &reply)
	if err != nil {
		panic(err)
	}
	fmt.Println(reply)

}
